#include <std.h>
#include <system.h>
#include <pio.h>
#include <error.h>
#include <pipe.h>
#include <type.h>
#include <dint.h>

int init_dint(); 
static u32 gid;

kdrv dint={
	.global_id=0,
	.name="dint",
	.uptime=0,
	.failures=0,
	.init=init_dint,
	.reset=NULL,
	.deinit=NULL,
	.select_subdevice=NULL,
	.read_char=NULL,
	.read_block=NULL,
	.write_char=NULL,
	.write_block=NULL,
};

int register_driver(kdrv *x)
{
	struct reg_drv *temp;
	add_node_end(struct reg_drv,head,temp);
	temp->driver=x;
	temp->next=NULL;
	get_sched_time(&x->uptime);
	x->failures=0;
	x->global_id=++gid; 
	printf("\nRegistered:%s",x->name);
}

int start_dev(kdrv *x)
{
	int r=3;
	while(--r && x->init());
}

kdrv *get_driver(char *name)
{
	struct reg_drv *temp=head;
	while(temp!=NULL && strcmp(temp->driver->name,name))	{
		temp=temp->next;
	}
	if(temp==NULL)
		return NULL;
	return temp->driver;
}

int display_registered()
{
	struct reg_drv *temp;
	for(temp=head;temp!=NULL;temp=temp->next)	{
		printf("[%s]",temp->driver->name);
	}
}

extern kdrv kbd;
extern kdrv mouseps2;
extern kdrv biosdisk;
extern kdrv fdc;
extern kdrv ptman;
extern kdrv diskfmt;
extern kdrv vbe;
extern kdrv lptdrv;
extern kdrv apm;
int init_dint()
{
	printf("\nStarting generalized driver interface..");
	gid=0;
	head=(struct reg_drv *)get_mem(sizeof(struct reg_drv));
	head->driver=&dint;
	head->next=NULL;
	register_driver(&kbd);
	start_dev(&kbd);
	register_driver(&mouseps2);
	start_dev(&mouseps2);
	register_driver(&biosdisk);
	start_dev(&biosdisk);
	register_driver(&fdc);
	start_dev(&fdc);
	register_driver(&ptman);
	start_dev(&ptman);
	register_driver(&diskfmt);
	start_dev(&diskfmt);
	register_driver(&vbe);
	start_dev(&vbe);
	register_driver(&lptdrv);
	start_dev(&lptdrv);
	register_driver(&apm);
	start_dev(&apm);
}

